<template>
  <div class="good-card">
    <img draggable="false" class="thumb" :src="data.thumb" />
    <div class="info">
      <div class="title">{{ data.title }}</div>
      <div
        class="price"
        :style="{ color: priceColor }"
        :class="data.storeName || data.subInfo ? 'above-info' : ''"
      >
        <div class="uinit">{{ priceUnit }}</div>
        <div class="number">{{ parseFloat(data.price + "").toFixed(2) }}</div>
        <div class="del" v-if="data.delPrice">
          <div class="unit">{{ priceUnit }}</div>
          <div class="number">
            {{ parseFloat(data.delPrice + "").toFixed(2) }}
          </div>
          <div class="line"></div>
        </div>
      </div>
      <div v-if="data.storeName || data.subInfo" class="sub-info">
        <AfastIcon
          v-if="data.storeName"
          :size="13"
          fill-color="var(--primary-color)"
          name="store"
        ></AfastIcon>
        <div v-if="data.storeName" class="store-name">{{ data.storeName }}</div>
        <div v-if="data.storeName && data.subInfo" class="div">|</div>
        <div v-if="data.subInfo">{{ data.subInfo }}</div>
      </div>
      <AfastButton
        v-if="showAddCart"
        class="add-cart"
        type="primary"
        round
        icon="increase"
        @click="handleAddCart($event)"
      >
      </AfastButton>
    </div>
  </div>
</template>
<script setup lang="ts">
import AfastIcon from "./AfastIcon.vue";
import AfastButton from "./AfastButton.vue";
import { useAddCartEffect } from "@/hooks/useAddCartEffect";
import { increaseCount } from "@/hooks/useCounter";
const emit = defineEmits(["add-cart", "after-add-cart-effect"]);
const playAddCartEffect = useAddCartEffect();
const props = withDefaults(
  defineProps<{
    priceColor?: Color;
    priceUnit?: CurrencyUnit;
    data: GoodsData;
    showAddCart?: boolean;
    bindCounter?: string;
  }>(),
  {
    priceUnit: "¥",
    priceColor: "red",
    showAddCart: false,
  }
);
const handleAddCart = (e: PointerEvent) => {
  if (playAddCartEffect.value) {
    playAddCartEffect.value(e.x, e.y, () => {
      if (props.bindCounter) {
        increaseCount(props.bindCounter);
      }
      emit("after-add-cart-effect");
    });
  }
  emit("add-cart", props.data);
};
</script>
<style lang="scss">
.good-card {
  user-select: none;
  width: 230px;
  min-height: 300px;
  cursor: pointer;
  background: var(--default-color);

  .thumb {
    object-fit: fill;
    width: 100%;
    height: 160px;
    background: gray;
  }

  .info {
    height: 120px;
    width: 100%;
    margin-top: 10px;
    box-sizing: border-box;
    position: relative;
    margin-bottom: 10px;

    .title {
      margin-left: 10px;
      overflow: hidden;
      max-lines: 3;
      word-wrap: break-word;
      font-size: 15px;
    }

    .sub-info {
      margin-left: 10px;
      align-items: center;
      display: flex;
      position: absolute;
      bottom: 0;
      font-size: 13px;
      color: gray;

      .store-name {
        margin-left: 2px;
      }

      .div {
        font-size: 9px;
        margin: 0 5px;
      }
    }

    .add-cart {
      position: absolute;
      right: 10px;
      bottom: 0;
      width: 25px;
      height: 25px;
    }

    .price {
      position: absolute;
      margin-left: 10px;
      align-items: end;
      margin-top: 10px;
      display: flex;
      bottom: 0;

      .unit {
        line-height: 19px;
        font-size: 19px;
        font-weight: bold;
      }

      .number {
        font-weight: bold;
        line-height: 19px;
        font-size: 19px;
        margin-left: 2px;
      }

      .del {
        color: gray;
        display: flex;
        align-items: end;
        margin-left: 5px;
        position: relative;

        .unit {
          line-height: 11px;
          font-size: 11px;
        }

        .number {
          line-height: 11px;
          font-size: 11px;
        }

        .line {
          width: 110%;
          height: 1px;
          background: gray;
          position: absolute;
          margin: auto;
          top: 3px;
          bottom: 0;
        }
      }
    }

    .price.above-info {
      bottom: 25px;
    }
  }
}

.good-card:hover {
  box-shadow: 1px 1px 10px #ccc;
}
</style>
